<%--
  Created by IntelliJ IDEA.
  User: 墨墨之家
  Date: 2021/11/4
  Time: 15:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <%@include file="/WEB-INF/common/head.jsp" %>
    <link rel="stylesheet" href="ztree/zTreeStyle.css">
    <script type="text/javascript" src="ztree/jquery.ztree.all-3.5.min.js"></script>
    <script type="text/javascript" src="crowd/menu.js"></script>
    <script type="text/javascript" src="crowd/my-role.js"></script>
    <script type="text/javascript">
        $(function () {
            // 全局变量
            window.pageNum = 1;
            window.pageSize = 10;
            window.keyword = "";

            // 填充tbody主体 和分页导航条
            generatePage();
            // 根据keyword的值刷新页面
            $("#searchBtn").click(function () {
                window.keyword = $("#seachValue").val();
                generatePage();
            })
            // 添加角色打开模态框
            $("#addRoleBtn").click(function () {
                $("#addRoleDiv").modal("show");
            });
            // 添加角色
            $("#saveRole").click(function () {
                var roleName = $.trim($("#addRoleDiv [name=roleName]").val());
                $.ajax({
                    url: "role/to/save.json",
                    data: "roleName=" + roleName,
                    type: "post",
                    success: function (data) {
                        if (data == "SUCCESS") {
                            layer.msg("添加成功");
                            window.pageNum = 999999;
                            generatePage();
                        } else {
                            layer.msg("添加失败");
                        }
                    },
                    dataType: "text"
                })
                $("#addRoleDiv").modal("hide");
                $("div [name=roleName]").val("");
            })
            // 更新用户时打开的模态框
            $("#rolePageBody").on("click", "tr [class='btn btn-primary btn-xs']", function () {
                var id = $(this).attr("id");
                $.getJSON("role/get/edit.json", "id=" + id, function (response) {
                    var role = response.data;
                    $("#roleIdIpt").val(role.id);
                    $("#nameInput").val(role.name);
                    $("#editRoleDiv").modal("show");
                })
            })
            // 更新用户
            $("#updateRole").click(function () {
                var id = $("#roleIdIpt").val();
                var name = $("#nameInput").val();
                $.ajax({
                    url: "role/to/update.json",
                    data: {
                        id: id,
                        name: name
                    },
                    type: "post",
                    success: function (response) {
                        if (response == "SUCCESS") {
                            layer.msg("修改成功")
                            generatePage()
                        } else {
                            layer.msg("修改失败")
                        }
                    },
                    dataType: "text"
                })
                $("#editRoleDiv").modal("hide");
            });
            // 进行单条删除
            $("#rolePageBody").on("click", "tr [class='btn btn-danger btn-xs']", function () {
                var name = $(this).attr("roleName")
                var id = $(this).attr("id");
                var roleArray = [{
                    roleName: name,
                    roleId: id
                }]
                showConfirmMedal(roleArray);
            })
            // 发送ajax请求给服务器进行角色删除
            $("#removeRole").click(function () {
                var ids = JSON.stringify(window.roleList);
                $.ajax({
                    url: "role/remove/by/id.json",
                    data: ids,
                    type: "post",
                    contentType: "application/json;charset=UTF-8",
                    success: function (result) {
                        if (result.result = "SUCCESS") {
                            layer.msg("操作成功")
                            generatePage()
                        } else {
                            layer.msg("操作失败")
                        }
                    },
                    dataType: "json",
                })
                $("#removeRoleDiv").modal("hide");
            });
            // 给全选按钮进行绑定
            $("#checkIpt").click(function () {
                var checked = this.checked;
                $(".itemBox").prop("checked", checked);
            });
            // 给全选按钮添加级联赋值
            $("#rolePageBody").on("click", ".itemBox", function () {
                var checks = $(".itemBox:checked").length;
                var noChecks = $(".itemBox").length;
                $("#checkIpt").prop("checked", checks == noChecks);
            });
            // 批条进行删除
            $("#batchRemove").click(function () {
                var itemBoxs = $(".itemBox:checked");
                var roleArray = [];
                itemBoxs.each(function () {
                    var roleId = this.id;
                    var roleName = $(this).parent().next().text();
                    roleArray.push({
                        "roleId": roleId,
                        "roleName": roleName
                    });
                });
                if (roleArray.length == 0) {
                    layer.msg("请至少选择一个再进行删除");
                    return;
                }
                showConfirmMedal(roleArray);
                $("#checkIpt").prop("checked", false);
            });

            // 给全部修改权限按钮绑定单击事件
            $(".checkBtn").click(function () {
                window.roleId = this.id;
                $("#assignModal").modal("show");
                fillTreeAuth();
            })

            // 对已经选好的绑定单击相应函数
            $("#assignBtn").click(function () {
                // 创建一个数组，存放authId
                var authIdList = [];

                // 得到ztreeobj
                var zTreeObj = $.fn.zTree.getZTreeObj("authTreeDemo");

                // 通过ztreeobj获取被选中得对话框
                var checkedNodes = zTreeObj.getCheckedNodes();

                // 遍历选中得节点
                for (var i = 0; i < checkedNodes.length; i++) {

                    var checkedNode = checkedNodes[i];

                    var authId = checkedNode.id;
                    // 把选中节点得id依次放入authidlist数组中
                    authIdList.push(authId)
                }
                // 这里的转换格式一定要确认！！！真坑人！！！
                var request = {
                    "authIdList": authIdList,
                    "roleId": [window.roleId]
                }
                var requestBody = JSON.stringify(request);
                console.log(requestBody)
                // 发送ajax请求到服务器，删除authIdList数组中id包含得内容
                $.ajax({
                    "url": "edit/to/auth/page.json",
                    "data": requestBody,
                    "type": "post",
                    "dataType": "json",
                    "contentType": "application/json;charset=UTF-8",
                    success: function (response) {
                        var result = response.result;
                        if (result == "SUCCESS") {
                            layer.msg("操作成功");
                        } else {
                            layer.msg(response.massage)
                        }
                    }
                })
                // 关闭模态框
                $("#assignModal").modal("hide");
            })

        })
    </script>
</head>
<body>
<%--静态包含--%>
<%@include file="/WEB-INF/common/nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%--静态包含--%>
        <%@include file="/WEB-INF/common/sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="seachValue" class="form-control has-success" type="text"
                                       placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="button" id="searchBtn" class="btn btn-warning"><i
                                class="glyphicon glyphicon-search"></i> 查询
                        </button>
                    </form>
                    <button type="button" id="batchRemove" class="btn btn-danger" style="float:right;margin-left:10px;">
                        <i class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <button type="button" id="addRoleBtn" class="btn btn-primary" style="float:right;"
                    ><i class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">序号</th>
                                <th width="30"><input type="checkbox" id="checkIpt"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody">

                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="pagination" class="pagination"></div>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="/WEB-INF/model-role-add.jsp" %>
<%@include file="/WEB-INF/model-role-edit.jsp" %>
<%@include file="/WEB-INF/model-role-remove.jsp" %>
<%@include file="/WEB-INF/modal-role-assign-auth.jsp" %>
</body>
</html>
